﻿function NodeContainer(position, size, color, parmas) {
    if (color == undefined) {
        color = "transparent";
    }
    if (parmas != undefined) {
        if (parmas.r == undefined) {
            parmas.r = "7.5";
        }
        if (parmas.dasharray == undefined) {
            dasharray = "0";
        }
    } else {
        parmas = { r: 7.5, dasharray: 0 };
    }

    var rect = svg.append("rect")
        .attr("rx", parmas.r)
        .attr("ry", parmas.r)
        .attr("fill", color)
        .attr("stroke", "black")
        .attr("stroke-width", "1")
        .attr("cursor", "move")
        .attr("stroke-dasharray", parmas.dasharray);

    BaseNode.apply(this, [rect, position, size]);

    this.children = [];

    this.addChildren = function (node) {
        this.children.push(node);
        node._parent = this;
    }

    this.hide = function () {
        this._domEl.attr("display", "none");
        for (var i = 0; i < this.children.length; i++) {
            this.children[i].hide();
        }
    }

    this.onUpdateLayout = function () {
        rect.attr("x", this.getActualPosition().x)
            .attr("y", this.getActualPosition().y)
            .attr("width", this.getSize().w)
            .attr("height", this.getSize().h);

        for (var i = 0; i < this.children.length; i++) {
            this.children[i].updateLayout();
        }
        if (this.logFlag) {
            worker.postMessage("Create a container:  x:" + this._position.x + ", y:" + this._position.y);
        }
        this.logFlag = false;
    }

    this.hitTest = function (pos) {
        var actualPosition = this.getActualPosition();
        if (pos.x > actualPosition.x
            && pos.y > actualPosition.y
            && pos.x < actualPosition.x + this.getSize().w
            && pos.y < actualPosition.y + this.getSize().h) {
            return new HitResult(true, actualPosition.x - pos.x, actualPosition.y - pos.y);
        }
        return new HitResult(false, 0, 0);
    }
}